package com.example.project.projectspringcloud.controller.front.privatecustom;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.controller.BaseController;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.sys.Area;
import com.example.project.projectspringcloud.entity.user.Collect;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.service.sys.SysAreaService;
import com.example.project.projectspringcloud.service.user.UserCollectService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.RedisUtil;
import com.example.project.projectspringcloud.utils.StringUtil;
import com.example.project.projectspringcloud.utils.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 私人定制
 */
@Api(tags = "私人定制控制器")
@Controller
@RequestMapping(value = "/private")
public class PrivateCustomController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private SysAreaService sysAreaService;

    @Autowired
    private UserCollectService userCollectService;

    /**
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "私人定制列表", notes = "返回私人定制列表页面")
    @GetMapping(value = {"", "/list"})
    public String list(User user, Model model, HttpServletRequest request, HttpServletResponse response) {
        user.setCollectUser(super.getCurrentUserId());
        // 获取摄影师
        user.setType(4);
        user.setOrderStr("");
        List<User> photographers = userService.list(isMobile() ? new Page(0, 6) : new Page(0, 12), user);
        // 获取编剧
        user.setType(5);
        List<User> scriptwriters = userService.list(isMobile() ? new Page(0, 6) : new Page(0, 12), user);
        model.addAttribute("photographers", new Page<User>(photographers));
        model.addAttribute("scriptwriters", new Page<User>(scriptwriters));

        if (super.getCurrentUser() != null) {
            // 获取当前登录用户收藏的摄影师/编剧列表
            List<Collect> collects = userCollectService.list(null, new Collect(getCurrentUser().getId(), 1));
            model.addAttribute("collects", collects);
        }

        if (StringUtils.isBlank(WebUtil.getCookie(request, "show-help"))) {
            // 设置用户浮窗显示, 过期为7天
            WebUtil.setCookie(response, "show-help", "true", 7);
        }

        Area entity = new Area();
        entity.setIsOpen(1);
        List<Area> areas = sysAreaService.list(entity, null);
        model.addAttribute("areas", areas);

        model.addAttribute("user", user);
        return isMobile() ? "m/content/private-custom" : "content/private-custom";
    }
}
